<!doctype html>
<html lang="zh-CN"><head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script>
    <title>薛尧的博客</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="" />

    
    
    
    <link rel="stylesheet" href="../../../css/theme.min.css">

    
    
    
    
    <link rel="stylesheet" href="../../../css/custom.min.css">
    

    
</head>
<body>
        <div id="content" class="mx-auto"><header class="container mt-sm-5 mt-4 mb-4 mt-xs-1">
    <div class="row">
        
        <div class="col-sm-4 col-12 text-sm-right text-center pt-sm-4">
            <a href="../../../" class="text-decoration-none">
                <img id="home-image" class="rounded-circle"
                    
                        src="https://ueyao.github.io/image-hosting/blog/avatar.png"
                    
                />
            </a>
        </div>
        <div class="col-sm-8 col-12 text-sm-left text-center">
        
            <h2 class="m-0 mb-2 mt-4">
                <a href="../../../" class="text-decoration-none">
                    
                        薛尧
                    
                </a>
            </h2>
            <p class="text-muted mb-1">
                
                    Java Developer | Short Video Creator
                
            </p>
            <ul id="nav-links" class="list-inline mb-2">
                
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../about/" title="关于">关于</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../post/" title="文章">文章</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../categories/" title="分类">分类</a>
                    </li>
                
            </ul>
            <ul id="nav-social" class="list-inline">
                
                    <li class="list-inline-item mr-3">
                        <a href="http://github.com/flowstone" target="_blank">
                            <i class="fab fa-github fa-1x text-muted"></i>
                        </a>
                    </li>
                
                    <li class="list-inline-item mr-3">
                        <a href="mailto:xueyao.me#gmail.com" target="_blank">
                            <i class="fas fa-at fa-1x text-muted"></i>
                        </a>
                    </li>
                
            </ul>
        </div>
    </div>
    <hr />
</header>
<div class="container">
    <div class="pl-sm-2">
        <div class="mb-3">
            <h3 class="mb-0">Docker入门-搭建docker私有仓库</h3>
            
            <small class="text-muted">发布于 2019-08-16</small>
        </div>

        <article>
            <h2 id="docker-hub">Docker Hub</h2>
<p>目前Docker官方维护了一个公共仓库Docker Hub，其中已经包括了数量超过15000个镜像。大部分需求都可以通过在Docker Hub中直接下载镜像来使用。</p>
<h3 id="注册登录">注册登录</h3>
<p>可以在https://hub.docker.com 免费注册一个Docker账号。在命令行执行docker login输入用户名及密码来完成在命令行界面登记Docker Hub。你可以通过docker logout退出登录。</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-01.png" alt=""></p>
<h3 id="拉取镜像">拉取镜像</h3>
<p>可以通过docker search命令来查找官方仓库中的镜像，并利用docker pull命令来将它下载到本地。</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-02.png" alt=""></p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-03.png" alt=""></p>
<h3 id="推送镜像">推送镜像</h3>
<p>用户也可以在登录后通过docker push命令来将自己的镜像推送到Docker Hub。</p>
<p>修改本地镜像的名字为账号名/镜像名</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-04.png" alt=""></p>
<p>上传镜像到公共仓库</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-05.png" alt=""></p>
<p>上传过后，查看远程公共仓库</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-06.png" alt=""></p>
<h2 id="私有仓库">私有仓库</h2>
<p>有时候使用Docker Hub这样的公共仓库可能不方便，用户可以创建一个本地仓库供私人使用。比如，基于公司内部项目构建的镜像。</p>
<p>docker-registry是官方提供的工具，可以用于构建私有的镜像仓库。</p>
<p>安装运行docker-registry</p>
<p>可以通过获取官方registry镜像来运行。默认情况下，仓库会被创建在容器的/var/lib/registry目录下。可以通过-v参数来将镜像文件存放在本地的指定路径。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker run --name registry -d  -p 5000:5000 --restart<span style="color:#f92672">=</span>always  -v /opt/data/registry:/var/lib/registry registry
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-07.png" alt=""></p>
<p>在私有仓库上传、搜索、下载镜像</p>
<p>创建好私有仓库之后，就可以使用docker tag来标记一个镜像，然后推送它到仓库。先在本机查看已有的镜像。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker image ls
</span></span></code></pre></div><p>使用docker tag将session-web:latest这个镜像标记为127.0.0.1:5000/session-web:latest格式为docker tag IMAGE[:TAG][REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker tag session-web:latest 127.0.0.1:5000/session-web:latest
</span></span></code></pre></div><p>使用docker push上传标记的镜像</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker push 127.0.0.1:5000/session-web:latest
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-08.png" alt=""></p>
<p>用curl查看仓库中的镜像</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>curl 127.0.0.1:5000/v2/_catlog
</span></span></code></pre></div><p>如果可以看到{&ldquo;repositories&rdquo;:[&ldquo;session-web&rdquo;]},表明镜像已经被成功上传了。</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-09.png" alt=""></p>
<p>先删除已有镜像，再尝试从私有仓库中下载这个镜像。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker image rm 127.0.0.1:5000/session-web:latest
</span></span><span style="display:flex;"><span>docker pull 127.0.0.1:5000/session-web:latest
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-repository-10.png" alt=""></p>
<h3 id="注意事项">注意事项</h3>
<p>如果不想使用127.0.0.1:5000作为仓库地址，比如想让本网段的其他主机也能把镜像推送到私有仓库。你就得把例如192.168.1.1:5000这样的内网地址作为私有仓库地址，这时你会发现无法成功推送镜像。</p>
<h4 id="可以用下面方式解决">可以用下面方式解决</h4>
<p>对于使用systemd的系统，请在/etc/docker/daemon.json中写入如下内容(如果文件不存在请新建该文件)</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">​</span>    <span style="color:#f92672">&#34;registry-mirror&#34;</span>:[
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">​</span>        <span style="color:#e6db74">&#34;http://hub-mirror.c.163.com&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">​</span>    ],
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">​</span>    <span style="color:#f92672">&#34;insecure-registries&#34;</span>:[
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">​</span>        <span style="color:#e6db74">&#34;192.168.1.1:5000&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">​</span>    ]
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div>
        </article>
    </div>

    <div id="disqus_thread"></div>
<script>
    window.disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xie-yao-de-bo-ke" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

            </div>
        </div><footer class="text-center pb-1">
    <small class="text-muted">
        
        &copy; Copyright 2024
        
        |
        <a href="https://beian.miit.gov.cn/" target="_blank">苏ICP备14012079号</a>
        <br />
        由 <a href="https://gohugo.io/" target="_blank">Hugo</a> 强力驱动
        | 主题 <a href="https://github.com/austingebauer/devise" target="_blank">Devise</a>
        <br />

    </small>
</footer></body>
</html>
